<?

$DB_TYPE_ID_MYSQL = 1;
$DB_TYPE_ID_ACCESS = 2;
$DB_TYPE_ID_ORACLE = 3;  

/**
 * dbconn
 *
 * A basic database abstraction class that supports
 * MySQL, Oracle, and MS Access.
 *
 *@author Chris Wetherell
 *@version 1.0
 *@copyright none
 */
class db_connection {
  protected $dbuser;
  protected $dbpath;
  protected $dbname;
  protected $dbpassword;
  protected $dbtype;
  protected $dbconn;
  protected $dbresults;
    
  function init($dbtype, $dbpath, $dbname, $dbuser, $dbpassword){
  	$this->dbtype = $dbtype;
  	$this->dbpath = $dbpath;
  	$this->dbname = $dbname;
  	$this->dbuser = $dbuser;
  	$this->dbpassword = $dbpassword;  		  	
  }
  
  function setDatabasePath($path) {$this->dbpath=$path;}
  
  function setUsername($dbuser) {$this->dbuser=$dbuser;}
  
  function setPassword($dbpassword) {$this->dbpassword=$dbpassword;}
  
  function setDatabaseName($dbname) {$this->dbname=$dbname;}  
  
  function getAccessConn() {
    $dbstring="".
      "Provider=Microsoft.Jet.OLEDB.4.0; Jet OLEDB:".
      "Database Password=".$this->dbpassword.";".
      "Data Source=".$this->dbpath; 
  
    $conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 
    $conn->Open($dbstring);  
    
    if (!$conn) {
      return false;
    }
    $this->dbtype = $GLOBALS["DB_TYPE_ID_ACCESS"];
    $this->dbresults = new Results($this->dbtype);
    $this->dbconn = $conn;
  } 
  
  function getMySqlConn() {
    $conn=mysql_connect ($this->dbpath, $this->dbuser, $this->dbpassword); 
    if (!$conn) { 
      echo mysql_error(); exit; 
    } else { 
      $db_select = mysql_select_db($this->dbname); 
      if (!$db_select) { 
        echo mysql_error(); exit; 
      } else { } 
    }
    $this->dbtype = $GLOBALS["DB_TYPE_ID_MYSQL"];
    $this->dbresults = new Results($this->dbtype);
    $this->dbconn = $conn;
  }
  
  function getOracleConn() {
    $conn = ocilogon($this->dbuser,$this->dbpassword,$this->dbname);
    $this->dbtype = $GLOBALS["DB_TYPE_ID_ORACLE"];
    $this->dbresults = new Results($this->dbtype);
    $this->dbconn = $conn;
  }
    
  function getLastInsertedId() {
    if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
    {
      return mysql_insert_id();
    }
    if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
    {
      $conn = $this->dbconn;
      $query = "SELECT @@IDENTITY as lastId ;";
      $conn->executeQuery($query);
      $results = $conn->getQueryResults();
      $row = $results->getRow();
      return $row->getField("lastId");
    }
    if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
    {
      // TODO: Add support for Oracle DBs
    }
  }
  
  function executeQuery($query) {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
      $res = $this->executeMySqlQuery($query);
      $this->dbresults = new Results($this->dbtype); 
      $this->dbresults->setResults($res);
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
       $this->dbresults->setResults($this->executeAccessQuery($query));
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
       $this->dbresults->setResults($this->executeOracleQuery($query));
     }
  }
  function executeAccessQuery($query) {
    return $this->dbconn->Execute($query);  
  }
  function executeMySqlQuery($query) {
    $res = mysql_query($query) or die( "Unable to complete task. Reason: " .mysql_error() );
    return $res;
  }
  function executeOracleQuery($query) {
    $stmt = ociparse($this->dbconn,$query);
    ociexecute($stmt,OCI_DEFAULT);
    return $stmt;
  }
  function getQueryResults() {
    return $this->dbresults;
  }
}

?>